package constant.milk.aptprice.dialog;

import constant.milk.aptprice.R;
import constant.milk.aptprice.util.MilkUtil;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class NumberDialog extends Dialog
{
	public static final int COUNT_FLAG = 0;
	public static final int MONEY_FLAG = 1;
	
	private Context context;
	private NumberCollBank collBank;
	private int flag;
	
	private TextView titleTextView;
	private TextView shareTextView;
	private TextView titleHanTextView;
	
	private TextView number1TextView;
	private TextView number2TextView;
	private TextView number3TextView;
	private TextView number4TextView;
	private TextView number5TextView;
	private TextView number6TextView;
	private TextView number7TextView;
	private TextView number8TextView;
	private TextView number9TextView;
	private TextView number0TextView;
	
	private TextView cencelTextView;
	private TextView backTextView;
	private TextView checkTextView;
	
	public NumberDialog(Context context, NumberCollBank collbank, int flag)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		this.context = context;
		this.collBank = collbank;
		this.flag = flag;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.number_dialog);
		
		setFindView();
		setOnClick();
		setData();
	}

	private void setFindView()
	{
		titleTextView = (TextView) findViewById(R.id.numberTitleTextView);
		shareTextView = (TextView) findViewById(R.id.numberShareTextView);
		titleHanTextView = (TextView) findViewById(R.id.numberTitleHanTextView);
		
		number1TextView = (TextView) findViewById(R.id.number1TextView);
		number2TextView = (TextView) findViewById(R.id.number2TextView);
		number3TextView = (TextView) findViewById(R.id.number3TextView);
		number4TextView = (TextView) findViewById(R.id.number4TextView);
		number5TextView = (TextView) findViewById(R.id.number5TextView);
		number6TextView = (TextView) findViewById(R.id.number6TextView);
		number7TextView = (TextView) findViewById(R.id.number7TextView);
		number8TextView = (TextView) findViewById(R.id.number8TextView);
		number9TextView = (TextView) findViewById(R.id.number9TextView);
		number0TextView = (TextView) findViewById(R.id.number0TextView);
		
		cencelTextView = (TextView) findViewById(R.id.numberCencelTextView);
		backTextView = (TextView) findViewById(R.id.numberBackTextView);
		checkTextView = (TextView) findViewById(R.id.numberCheckTextView);
	}

	private void setOnClick()
	{
		number1TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("1");
			}
		});
		
		number2TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("2");
			}
		});
		
		number3TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("3");
			}
		});
		
		number4TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("4");
			}
		});
		
		number5TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("5");
			}
		});
		
		number6TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("6");
			}
		});
		
		number7TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("7");
			}
		});
		
		number8TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("8");
			}
		});
		
		number9TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("9");
			}
		});

		number0TextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("0");
			}
		});
		
		cencelTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextCencel();
			}
		});
		
		backTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextDelete();
			}
		});
		
		checkTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String number = titleTextView.getText().toString();
				
				collBank.getNumber(MilkUtil.getCommaDel(number));
				dismiss();
			}
		});
	}

	private void setData()
	{
		if (flag == COUNT_FLAG)
		{
			shareTextView.setText("평");
			titleHanTextView.setVisibility(View.GONE);
		}
		else
		{
			shareTextView.setText("원");
			titleHanTextView.setVisibility(View.VISIBLE);
			titleHanTextView.setText("빵원");
		}
		
		titleTextView.setText("0");
	}
	
	private void setTextInput(String number)
	{
		String text = titleTextView.getText().toString();
		
		text = text + number;
		
		if (flag == COUNT_FLAG)
		{
			if (getThreeLengthCheck(text))
			{
				MilkUtil.showToast(context, "3자리 이상 할 수 없습니다.");
				return;
			}
		}
		else
		{
			titleHanTextView.setText(MilkUtil.makeMoneyHangul(MilkUtil.getCommaDel(text)) + "원");
		}
		
		text = MilkUtil.makeStringWithComma(text);
		
		if (text.equals("0"))
		{
			if (flag == MONEY_FLAG)
			{
				titleHanTextView.setText("빵원");
			}
		}
		
		titleTextView.setText(text);
	}
	
	public boolean getThreeLengthCheck(String text)
	{
		if (text.length() > 3)
		{
			return true;
		}
		
		return false;
	}
	
	private void setTextDelete()
	{
		String text = titleTextView.getText().toString();
		
		if (text.length() < 2)
		{
			titleTextView.setText("0");
			
			if (flag == MONEY_FLAG)
			{
				titleHanTextView.setText("빵원");
			}
			
			return;
		}
		
		text = text.substring(0, text.length()-1);
		
		if (flag == MONEY_FLAG)
		{
			titleHanTextView.setText(MilkUtil.makeMoneyHangul(MilkUtil.getCommaDel(text)) + "원");
		}
		
		text = MilkUtil.makeStringWithComma(text);
		
		titleTextView.setText(text);
	}
	
	private void setTextCencel()
	{
		titleTextView.setText("0");
		
		if (flag == MONEY_FLAG)
		{
			titleHanTextView.setText("빵원");
		}
	}

	public interface NumberCollBank
	{
		public abstract void getNumber(String number);
	}
}
